home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 1 / your choice.zip / your choice / PRGMMING / VISIONIX / VMEMU.PAS < prev    next >
Pascal/Delphi Source File  |  1993-12-28  |  6KB  |  321 lines

  1. {
  2.  ════════════════════════════════════════════════════════════════════════════
  3.  
  4.  Visionix Memory Management Unit (VMEM)
  5.    Version 0.5
  6.  Copyright 1991,92,93 Visionix
  7.  ALL RIGHTS RESERVED
  8.  
  9.  ────────────────────────────────────────────────────────────────────────────
  10.  
  11.  Revision history in reverse chronological order:
  12.  
  13.  Initials  Date      Comment
  14.  ────────  ────────  ────────────────────────────────────────────────────────
  15.  
  16.  jrt       12/28/93  Added documentation
  17.  
  18.  jrt       11/01/93  Uncommented VMemZero stuff.
  19.  
  20.  mep       02/11/93  Cleaned up code for beta release
  21.  
  22.  jrt       02/08/93  Sync with beta 0.12 release
  23.  
  24.  jrt       12/07/92  Sync with beta 0.11 release
  25.  
  26.  jrt       11/21/92  Sync with beta 0.08
  27.  
  28.  jrt       08/01/92  First logged revision.
  29.  
  30.  ════════════════════════════════════════════════════════════════════════════
  31. }
  32.  
  33. (*-
  34.  
  35. [TEXT]
  36.  
  37. <Overview>
  38.  
  39. The VMemu unit implements 4 simple, cross-platform memory allocation
  40. functions.  This unit has been rewritten to include support for
  41. both Windows and OS/2 GlobalAlloc/GlobalLock/GlobalFree functions.
  42.  
  43. This unit will be replaced with the new unit in the next BETA release.
  44.  
  45. -*)
  46.  
  47. Unit VMemu;
  48.  
  49. Interface
  50.  
  51. Uses
  52.  
  53.   VTypesu;
  54.  
  55. {──────────────────────────────────────────────────────────────────────────────}
  56.  
  57. Const
  58.  
  59.   allocZERO      = $01;
  60.  
  61. Type
  62.  
  63.   TMemHeader = RECORD
  64.  
  65.     Locks    : LONGINT;
  66.     Size     : LONGINT;
  67.     Ptr      : Pointer;
  68.  
  69.   END;
  70.  
  71.   PMemHeader = ^TMemHeader;
  72.  
  73. {──────────────────────────────────────────────────────────────────────────────}
  74.  
  75.  
  76. Function  VMemAlloc(              Flags          : WORD;
  77.                                   Size           : LONGINT         ):THandle;
  78.  
  79.  
  80. Function  VMemLock(               MemHandle      : THandle         ):Pointer;
  81.  
  82.  
  83. Procedure VMemUnlock(             MemHandle      : THandle         );
  84.  
  85.  
  86. Procedure VMemFree(               MemHandle      : THandle         );
  87.  
  88.  
  89. {──────────────────────────────────────────────────────────────────────────────}
  90.  
  91. Implementation
  92.  
  93. {──────────────────────────────────────────────────────────────────────────────}
  94.  
  95. Procedure VMemZero( Mem : Pointer; Size : WORD );
  96.  
  97. Var
  98.  
  99.   TheSeg  : WORD;
  100.   TheOfs  : WORD;
  101.   TheSize : WORD;
  102.  
  103. BEGIN
  104.  
  105.   TheSeg  := Seg( Mem^ );
  106.   TheOfs  := Ofs( Mem^ );
  107.   TheSize := Size;
  108.  
  109.   ASM
  110.  
  111.     MOV ES, TheSeg
  112.     MOV DI, TheOfs
  113.  
  114.     MOV AX,0
  115.     MOV CX,TheSize
  116.  
  117.     CLD
  118.     REPZ STOSB
  119.  
  120.   END;
  121.  
  122. END;
  123.  
  124. {──────────────────────────────────────────────────────────────────────────────}
  125.  
  126. (*-
  127.  
  128. [FUNCTION]
  129.  
  130. Function  VMemAlloc(              Flags          : WORD;
  131.                                   Size           : LONGINT         ):THandle;
  132.  
  133.  
  134. [PARAMETERS]
  135.  
  136. Flags       Allocation control flags
  137.                 allocZERO zero out allocated memory.
  138.  
  139. Size        Size of the memory block to allocate, in bytes.
  140.  
  141. [RETURNS]
  142.  
  143. 0/NIL if an error,
  144. otherwise a valid Memory Handle,
  145.  
  146. [DESCRIPTION]
  147.  
  148. This function allocates a memory block and returns the handle of the
  149. memory block.  If the block could not be allocated, or an error
  150. occurred, this function will return a 0/nil.
  151.  
  152. -*)
  153.  
  154.  
  155. Function  VMemAlloc(              Flags          : WORD;
  156.                                   Size           : LONGINT         ):THandle;
  157.  
  158. Var
  159.  
  160.   NMH : PMemHeader;
  161.  
  162. BEGIN
  163.  
  164.   If Size + 1024 > MaxAvail Then
  165.   BEGIN
  166.  
  167.     VMemAlloc := NIL;
  168.  
  169.   END
  170.   ELSE
  171.   BEGIN
  172.  
  173.     New( NMH );
  174.  
  175.     NMH^.Locks := 0;
  176.     NMH^.Size  := Size;
  177.  
  178.     GetMem( NMH^.Ptr, Word( Size ) );
  179.  
  180.     If Flags and allocZERO >0 Then
  181.       VMemZero( NMH, Word( Size ) );
  182.  
  183.     VMemAlloc := NMH;
  184.  
  185.   END;
  186.  
  187. END;
  188.  
  189. {──────────────────────────────────────────────────────────────────────────────}
  190.  
  191. (*-
  192.  
  193. [FUNCTION]
  194.  
  195. Function  VMemLock(               MemHandle      : THandle         ):Pointer;
  196.  
  197. [PARAMETERS]
  198.  
  199. memhandle   memory handle obtained from a previous call to VMemAlloc
  200.  
  201. [RETURNS]
  202.  
  203. 0/NIL if an error,
  204. otherwise a pointer the locked memory block.
  205.  
  206. [DESCRIPTION]
  207.  
  208. This function locks down the memory block associated with "Memhandle"
  209. and returns a pointer to the memory block.
  210.  
  211. -*)
  212.  
  213.  
  214. Function  VMemLock(               MemHandle      : THandle         ):Pointer;
  215.  
  216. BEGIN
  217.  
  218.   If MemHandle<>NIL Then
  219.   BEGIN
  220.  
  221.     Inc( PMemHeader( MemHandle )^.Locks );
  222.  
  223.     VMemLock := PMemHeader( MemHandle )^.Ptr;
  224.  
  225.   END
  226.   ELSE
  227.   BEGIN
  228.  
  229.     VMemLock := NIL;
  230.  
  231.   END;
  232.  
  233. END;
  234.  
  235. {──────────────────────────────────────────────────────────────────────────────}
  236.  
  237. (*-
  238.  
  239. [FUNCTION]
  240.  
  241. Procedure VMemUnlock(             MemHandle      : THandle         );
  242.  
  243. [PARAMETERS]
  244.  
  245. memhandle   memory handle obtained from a previous call to VMemAlloc
  246.  
  247. [RETURNS]
  248.  
  249. nothing
  250.  
  251. [DESCRIPTION]
  252.  
  253. This function unlocks the memory block associated with "Memhandle".
  254.  
  255. -*)
  256.  
  257.  
  258.  
  259. Procedure VMemUnlock(             MemHandle      : THandle         );
  260.  
  261. BEGIN
  262.  
  263.   Dec( PMemHeader( MemHandle )^.Locks );
  264.  
  265. END;
  266.  
  267. {──────────────────────────────────────────────────────────────────────────────}
  268.  
  269. (*-
  270.  
  271. [FUNCTION]
  272.  
  273. Procedure VMemFree(               MemHandle      : THandle         );
  274.  
  275. [PARAMETERS]
  276.  
  277. memhandle   memory handle obtained from a previous call to VMemAlloc
  278.  
  279. [RETURNS]
  280.  
  281. nothing
  282.  
  283. [DESCRIPTION]
  284.  
  285. This function frees the memory block associated with "Memhandle".
  286.  
  287. -*)
  288.  
  289.  
  290.  
  291. Procedure VMemFree(               MemHandle      : THandle         );
  292.  
  293.   { VMemFree frees a memory handle and the memory associated with  }
  294.   { it.                                                            }
  295.   {                                                                }
  296.   { MemHandle           Previously allocated memory handle.        }
  297.  
  298. BEGIN
  299.  
  300.   { What to do is locks <> 0 ??? }
  301.  
  302.   If MemHandle<>NIL Then
  303.   BEGIN
  304.  
  305.     FreeMem( PMemHeader( MemHandle )^.Ptr,
  306.              PMemHeader( MemHandle )^.Size  );
  307.  
  308.     Dispose( PMemHeader( MemHandle ) );
  309.  
  310.   END;
  311.  
  312. END;
  313.  
  314. {──────────────────────────────────────────────────────────────────────────────}
  315. {──────────────────────────────────────────────────────────────────────────────}
  316. {──────────────────────────────────────────────────────────────────────────────}
  317.  
  318. BEGIN
  319.  
  320. END.
  321.